import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

// import store from '@/store';
let isLogin = sessionStorage.getItem('login')
// console.log(JSON.parse(store.state.userInfo));

// function hasUrl(url: string, next: any) {
//     let result = JSON.parse(store.state.userInfo).menus_url.some((item: any) => {
//         return url === item
//     })
//     // console.log(result);//true或false
//     if (result) {
//         next()
//     } else {
//         next('/home')
//     }

// }
// hasUrl('/menu', 1)


export const IndexRouter = [
    // {
    // path: '/',
    // component: () => import('../pages/index.vue'),
    // meta: {
    //     title: '1'
    // },
    // beforeEnter: (to: any, from: any, next: any) => {
    //     hasUrl('/menu', next)
    // }
    // }

]


const routes: Array<RouteRecordRaw> = [
    //一级路由重定向
    {
        path: '/:catchAll(.*)', redirect: '/login'
    },
    {
        path: '/index',
        component: () => import('../pages/index.vue'),
        // children: [
        //     {
        //         path: '/home',
        //         component: () => import('../views/home.vue'),
        //         meta: {
        //             title: '首页'
        //         }
        //     },
        // ]
    },
    {
        path: '/login',
        component: () => import('../pages/login.vue'),
        // children: [
        //     {
        //         path: '/home',
        //         component: () => import('../views/home.vue'),
        //         meta: {
        //             title: '首页'
        //         }
        //     },
        // ]
    },
    ...IndexRouter
]

const router = createRouter({
    history: createWebHistory(),
    routes
})
//路由全局守卫
router.beforeEach((to, from, next) => {
    //   console.log(to, 123, from);
    if (to.path == "/login") {//如果是去登录页就不拦截
        next()
        return
    } else if (isLogin) {//有登录信息没有事null
        next()
        return
    } else {
        next('/login')
    }
    next()
})




export default router
